<?php

namespace Admin\Controller;
use Think\Controller;
use Common\Model\OptionModel;
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class UpdatePlatformMoneyController extends Controller {

    public function updateMoney(){
        //更新恒指
        $this->updateOptionMoney();
        //更新欧指
        $this->updateEuroMoney();
		//伦敦金
		$this->updateGoldMoney();
    }

    public function updateOptionMoney(){
        $Model = new OptionModel("order");//实例化MongoDB
        //获取100条没有处理过的订单
        $where['platformed'] = array('neq',1);
        $list = $Model->where($where)->order('id asc')->limit(500)->select();
        //dump($list);
        //安装比例分配到两个账号
        foreach($list as $k=>$v){

            //数据
            $data = array();
            $data['name']  = $v['option_name'];
            $data['invest'] = floatval($v['invest']); 
            $data['o_user_id'] = intval($v['user_id']);
            $data['order_id'] = intval($v['id']);
            $data['type'] = 1;
            $data['create_time'] = time();

            //订单成本
            $m = $v['invest']*0.02;
            $m1 = $m*0.25;    //账号1金额
            $data['f_user_id'] = 6516; //账号1id
            $data['money'] = floatval($m1);
            //插入金额
            $LogModel = new OptionModel("platform");
            
            $res1 = $LogModel->add($data);
            $this->modify(6516,$m1,'恒指平台流水');


            $m2 = $m*0.75;    //账号2金额
            $data['f_user_id'] = 6051; //账号id
            $data['money'] = floatval($m2);

            $res2 = $LogModel->add($data);
            $this->modify(6051,$m2,'恒指平台流水');

            //将数据标记为已处理
            $Model->where(array('id'=>$v['id']))->save(array('platformed'=>1));

        }
       
    }

    public function updateEuroMoney(){
        $Model = new OptionModel("euro_order");//实例化MongoDB
        //获取100条没有处理过的订单
        $where['platformed'] = array('neq',1);
        $list = $Model->where($where)->order('id asc')->limit(500)->select();
        //dump($list);
        //安装比例分配到两个账号
        foreach($list as $k=>$v){

            //数据
            $data = array();
            $data['name'] = $v['option_name'];
            $data['invest'] = floatval($v['invest']); 
            $data['o_user_id'] = intval($v['user_id']);
            $data['order_id'] = intval($v['id']);
            $data['type'] = 2;
            $data['create_time'] = time();

            //订单成本
            $m = $v['invest']*0.02;
            $m1 = $m*0.25;    //账号1金额
            $data['f_user_id'] = 6516; //账号1id
            $data['money'] = floatval($m1);
            //插入金额
            $LogModel = new OptionModel("platform");
            
            $res1 = $LogModel->add($data);
            $this->modify(6516,$m1,'欧指平台流水');


            $m2 = $m*0.75;    //账号2金额
            $data['f_user_id'] = 6051; //账号id
            $data['money'] = floatval($m2);

            $res2 = $LogModel->add($data);
            $this->modify(6051,$m2,'欧指平台流水');

            //将数据标记为已处理
            $Model->where(array('id'=>$v['id']))->save(array('platformed'=>1));

        }
    }
	
	public function updateGoldMoney(){
        $Model = new OptionModel("goldorder");//实例化MongoDB
        //获取100条没有处理过的订单
        $where['platformed'] = array('neq',1);
        $list = $Model->where($where)->order('id asc')->limit(500)->select();
        //dump($list);
        //安装比例分配到两个账号
        foreach($list as $k=>$v){

            //数据
            $data = array();
            $data['name'] = $v['option_name'];
            $data['invest'] = floatval($v['invest']); 
            $data['o_user_id'] = intval($v['user_id']);
            $data['order_id'] = intval($v['id']);
            $data['type'] = 2;
            $data['create_time'] = time();

            //订单成本
            $m = $v['invest']*0.02;
            $m1 = $m*0.25;    //账号1金额
            $data['f_user_id'] = 6516; //账号1id
            $data['money'] = floatval($m1);
            //插入金额
            $LogModel = new OptionModel("platform");
            
            $res1 = $LogModel->add($data);
            $this->modify(6516,$m1,'伦敦金平台流水');


            $m2 = $m*0.75;    //账号2金额
            $data['f_user_id'] = 6051; //账号id
            $data['money'] = floatval($m2);

            $res2 = $LogModel->add($data);
            $this->modify(6051,$m2,'伦敦金平台流水');

            //将数据标记为已处理
            $Model->where(array('id'=>$v['id']))->save(array('platformed'=>1));

        }
    }


    private function modify($user_id,$money,$note){
        $where = array();
        $where['id'] = $user_id;
        $user = M('Follow')->field('id,money')->where($where)->find();
        //金额日志
        if($user){
            $total_money = $user['money']+$money;
            $res = M('Follow')->where(array('id'=>$user['id']))->setField(array('money'=>$total_money));
        }
        
    }


    public function updatePlatform(){

        $Option = new OptionModel("order");//实例化MongoDB
        $Option->where(array('platformed'=>1))->save(array('platformed'=>0));

        $Euro = new OptionModel("euro_order");//实例化MongoDB
        $Euro->where(array('platformed'=>1))->save(array('platformed'=>0));
    }

}